package com.zy.explore.search.interfaces.facade.v1;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zy.explore.search.application.service.SolrService;
import com.zy.explore.search.infrastructure.result.DisableResultVOAdvice;
import com.zy.explore.search.interfaces.frm.CommonIdFrm;
import com.zy.explore.search.interfaces.frm.ProductSolrFrm;
import com.zy.explore.search.interfaces.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 建类说明：solr门面
 *
 * @author 中国印
 * @date 2022年02月10日 00:43:04
 */
@Api(tags = {"solr门面"})
@RestController
@RequestMapping("/solr")
public class SolrFacade {

    @Resource
    private SolrService solrService;

    /**
     * 新增/修改 索引
     * 当 id 存在的时候, 此方法是修改(当然, 我这里用的 uuid, 不会存在的), 如果 id 不存在, 则是新增
     *
     */
    @ApiOperationSupport(order = 10, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("①保存产品数据[新增/修改]")
    @PostMapping("/saveBean")
    public ResultVO<String> saveBean(@RequestBody @Validated ProductSolrFrm frm) {
        return ResultVO.getSuccess(solrService.save(frm));
    }

    /**
     * 根据id删除索引
     *
     * @param frm solr主键ID对象
     */
    @ApiOperationSupport(order = 20, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("②删除数据")
    @DeleteMapping("/deleteById")
    public boolean deleteById(@RequestBody @Validated CommonIdFrm frm) {
        return solrService.deleteById(frm.getId());
    }

    /**
     * 删除所有的索引
     */
    @ApiOperationSupport(order = 30, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("③删除所有数据")
    @DeleteMapping("/deleteAll")
    public boolean deleteAll() {
        return solrService.deleteAll(null);
    }


    /**
     * 根据id查询索引
     */
    @ApiOperationSupport(order = 40, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("④根据主键查询数据")
    @GetMapping("/findById")
    public ResultVO<Map> findById(@SpringQueryMap @Validated CommonIdFrm frm) {
        return ResultVO.getSuccess(solrService.findById(frm.getId(), Map.class));
    }

    /**
     * 综合查询: 在综合查询中, 有按条件查询, 条件过滤, 排序, 分页, 高亮显示, 获取部分域信息
     */
    @DisableResultVOAdvice
    @ApiOperationSupport(order = 50, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("⑤根据条件查询数据集")
    @GetMapping("/search/{q}")
    public SolrDocumentList search(@PathVariable String q) {
        return solrService.search(q);
    }

    /**
     * 综合查询: 在综合查询中, 有按条件查询, 条件过滤, 排序, 分页, 高亮显示, 获取部分域信息
     *
     */
    @DisableResultVOAdvice
    @ApiOperationSupport(order = 60, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("⑥根据条件查询文档集")
    @GetMapping("/doc/search/{q}")
    public SolrDocumentList docSearch(@PathVariable String q) {
        return solrService.searchDoc(q);
    }

    /**
     * 文件创建索引
     */
    @ApiOperationSupport(order = 70, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("⑦文件创建索引")
    @GetMapping("/doc/dataImport")
    public boolean dataImport() {
        return solrService.dataImport();
    }
}
